//
//  DCProductionListViewController.m
//  HNBaseProject
//
//  Created by cc on 2024/7/22.
//

#import "DCProductionListViewController.h"
#import "DCProductionDetailViewController.h"
#import "DCMyProductionHistoryCell.h"
#import "YNSResultVC.h"
#import "AIMyLibraryShowCell.h"
@interface DCProductionListViewController ()<MxNavigationViewDelegate>
@property (nonatomic, strong) MxPublicNav *nav;

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet UIView *historyBg;
@property (nonatomic, strong) NSMutableArray *listArray;
@property (weak, nonatomic) IBOutlet UILabel *histooryLab;

@end

@implementation DCProductionListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.nav = [[MxPublicNav alloc] initWithFrame:CGRectMake(0, 0, kScreenW, kNavBarHeight)];
    self.nav.titleLab.text =  NSLocalizedString(@"历史制作", nil);//@"历史制作";
    self.histooryLab.text = NSLocalizedString(@"历史制作", nil);//@"历史制作";
    self.nav.delegate = self;
    [self.view addSubview:self.nav];
    
    self.listArray = [[NSMutableArray alloc] init];
    
    [self.historyBg mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.offset(kNavBarHeight);
        make.height.mas_equalTo(60);
    }];
    
    [self.collectionView registerClass:[AIMyLibraryShowCell class] forCellWithReuseIdentifier:@"AIMyLibraryShowCell"];
    [self.collectionView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.bottom.offset(0);
        make.left.offset(15);
        make.right.offset(-15);
        make.top.mas_equalTo(self.historyBg.mas_bottom);
    }];
    self.collectionView.showsVerticalScrollIndicator = NO;
    
    UICollectionViewFlowLayout *flowLayout = (UICollectionViewFlowLayout *)self.collectionView.collectionViewLayout;
    
    flowLayout.minimumLineSpacing = 15;
    flowLayout.minimumInteritemSpacing = 15;
    
    flowLayout.itemSize = CGSizeMake((kScreenW - 45) / 2.0, 165);
    [self getHistoryList];
}

- (void)getHistoryList {

    MJWeakSelf
    [[DCHttpClient shareClient] requestWithPath:@"api/video.video/historyVideo" params:@{} httpMethod:DCHttpRequestGet sucess:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        NSDictionary * dict = [responseObject mj_JSONObject];
        [weakSelf.view hideToastActivity];
        if([dict[@"code"] intValue]==1){
            [weakSelf.listArray removeAllObjects];
            NSArray *dataArray = [NSArray modelArrayWithClass:[AIMyHistoryCreateModel class] json:dict[@"data"][@"lists"]];
            [weakSelf.listArray addObjectsFromArray:dataArray];
            [weakSelf.collectionView reloadData];
        } else {

        }
    } failture:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        [weakSelf.view hideToastActivity];
    }];
}

- (void)leftTap {
    [self.navigationController popViewControllerAnimated:YES];
}


#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.listArray.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    AIMyHistoryCreateModel *model = self.listArray[indexPath.item];
    AIMyLibraryShowCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"AIMyLibraryShowCell" forIndexPath:indexPath];
    [cell reloadCellWithModel:model];
    return cell;
}

#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    AIMyHistoryCreateModel *model = self.listArray[indexPath.item];
    MJWeakSelf
    [self.view makeToastActivity];
    [[DCHttpClient shareClient] requestWithPath:@"api/video.video/detail" params:@{@"id":model.ID} httpMethod:DCHttpRequestGet sucess:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        NSDictionary * dict = [responseObject mj_JSONObject];
        [weakSelf.view hideToastActivity];
        if([dict[@"code"] intValue]==1){

            YNSResultVC *resultVc = [[YNSResultVC alloc] init];
            resultVc.video_url = dict[@"data"][@"video_url"];
            resultVc.cover_image_url = dict[@"data"][@"image_url"];
            resultVc.fromList = YES;
            [weakSelf.navigationController pushViewController:resultVc animated:YES];
        } else {

        }
    } failture:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        [weakSelf.view hideToastActivity];
    }];
}


@end
